<template>
  <div>
    <el-card class="box-card">
      <el-collapse v-model="activeName">
        <el-collapse-item name="1">
          <template slot="title"><span
              style="font-size:20px;font-family:Microsoft YaHei;font-weight: bold;">车辆列表</span></template>
          <el-divider></el-divider>
          <!-- 查询条件 -->
          <el-form :inline="true" :model="queryForm" style="text-align: left;">
            <el-form-item label="车辆名称">
              <el-input v-model="queryForm.name" placeholder="车辆名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="query" style="background-color: #c10003;color: aliceblue;" icon="el-icon-search">查询
              </el-button>
            </el-form-item>
            <el-form-item style="float: right;">
              <el-button @click="openAdd" style="background-color: #c10003;color: aliceblue;" icon="el-icon-plus">新增
              </el-button>
            </el-form-item>
          </el-form>
          <!-- 数据列表 -->
          <el-table :data="tableData" border style="width: 100%"
            :header-cell-style="{background:'#f7f7f7',color:'#666666'}">
            <el-table-column fixed prop="name" label="名称" align="center" width="100">
            </el-table-column>
            <el-table-column prop="brand" label="品牌" align="center" width="100">
            </el-table-column>
            <el-table-column prop="model" label="型号" align="center" width="100">
            </el-table-column>
            <el-table-column prop="box" label="厢数" align="center" width="70">
            </el-table-column>
            <el-table-column prop="volume" label="排量" align="center" width="70">
            </el-table-column>
            <el-table-column prop="door" label="门数" align="center" width="70">
            </el-table-column>
            <el-table-column prop="seat" label="座数" align="center" width="70">
            </el-table-column>
            <el-table-column prop="gears" label="档位" align="center" width="100">
            </el-table-column>
            <el-table-column prop="auto" label="档位类型" align="center" width="100">
            </el-table-column>
            <el-table-column prop="price" label="价格" align="center" width="100">
            </el-table-column>
            <el-table-column prop="status" label="状态" align="center" width="100">
            </el-table-column>
            <el-table-column prop="createId" label="新增人员" align="center" width="100">
            </el-table-column>
            <el-table-column prop="createTime" label="新增时间" align="center" width="200">
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" width="350">
              <template slot-scope="scope">
                <el-button @click="openPic(scope.row)" style="background-color: #c10003;color: aliceblue;"
                  icon="el-icon-camera">图片</el-button>
                <el-button @click="openEdit(scope.row)" style="background-color: #c10003;color: aliceblue;"
                  icon="el-icon-edit">编辑</el-button>
                <!-- <el-popconfirm title="确定删除吗?" @confirm="del(scope.row)">
                  <el-button slot="reference" style="background-color: #ff0000;color: aliceblue;" icon="el-icon-delete">
                    删除</el-button>
                </el-popconfirm> -->
                <el-button @click="openDel(scope.row)" style="background-color: #ff0000;color: aliceblue;"
                  icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination style="float: left;" v-show="total > 0" background @size-change="query" @current-change="query"
            hide-on-single-page :current-page.sync="queryForm.pageNum" :page-size.sync="queryForm.pageSize"
            :page-sizes="[2,5,8]" layout="total,sizes,prev, pager, next,jumper" :total="total">
          </el-pagination>
          <!-- 新增/修改页面 -->
          <el-dialog :title="title" :visible.sync="show" width="500px" style="text-align: left;">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px" size="small">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" clearable :disabled="disabled"></el-input>
              </el-form-item>
              <el-form-item label="品牌" prop="brand">
                <el-input v-model="form.brand" clearable></el-input>
              </el-form-item>
              <el-form-item label="型号" prop="model">
                <el-input v-model="form.model" clearable></el-input>
              </el-form-item>
              <el-form-item label="厢数">
                <el-select v-model="form.box" clearable placeholder="请选择" @change="$forceUpdate()">
                  <el-option v-for="item in boxlist" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="排量" prop="volume">
                <el-input v-model="form.volume" clearable></el-input>
              </el-form-item>
              <el-form-item label="门数">
                <el-select v-model="form.door" clearable placeholder="请选择" @change="$forceUpdate()">
                  <el-option v-for="item in doorlist" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="座数">
                <el-select v-model="form.seat" clearable placeholder="请选择" @change="$forceUpdate()">
                  <el-option v-for="item in seatlist" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="档位" prop="gears">
                <el-input v-model="form.gears" clearable></el-input>
              </el-form-item>
              <el-form-item label="档位类型">
                <el-select v-model="form.auto" clearable placeholder="请选择" @change="$forceUpdate()">
                  <el-option v-for="item in autolist" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="价格(元/天)" prop="price">
                <el-input v-model="form.price" type="peice" clearable></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="form.status" clearable placeholder="请选择" @change="$forceUpdate()">
                  <el-option v-for="item in statuslist" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="submitForm" style="background-color: #c10003;color: aliceblue;" icon="el-icon-check">提交
              </el-button>
              <el-button @click="show = false">取消</el-button>
            </span>
          </el-dialog>
          <!-- 删除页面 -->
          <el-dialog :title="title" :visible.sync="delshow" width="20%" style="text-align: left;top: 20%;">
            <span><i class="el-icon-warning"
                style="background-color: aliceblue;color: #ffaa00;"></i>将永久删除车辆信息，确认要删除吗?</span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="work = false">取消</el-button>
              <el-button @click="del(id)" style="background-color: #c10003;color: aliceblue;">确定</el-button>
            </span>
          </el-dialog>
          <!-- 图片页面 -->
          <el-dialog :title="title" :visible.sync="pic" width="500px" style="text-align: left;">
            <el-divider></el-divider>
            <el-image :src="'data:image/jpeg;base64,' + carImgUrl" fit="contain">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
            <el-divider></el-divider>
            <div style="padding: 14px;">
              <div class="bottom clearfix">
                <el-upload ref="upload" d :data='upData' :action="action" :headers="headers" :on-remove="handleRemove"
                  :limit="1" accept="image/jpeg" :before-upload="beforeUpload" :auto-upload="false"
                  :on-success="uploadSuccess" :on-error="uploadError">
                  <el-button slot="trigger" style="background-color: #c10003;color: aliceblue;"
                    icon="el-icon-picture-outline">选取图片</el-button>
                  <el-button style="background-color: #c10003;color: aliceblue;" icon="el-icon-upload"
                    @click="submitUpload">上传图片</el-button>
                  <el-button @click="pic = false">取消</el-button>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过5MB</div>
                </el-upload>
              </div>
            </div>
          </el-dialog>
          <!-- 操作页面 -->
          <el-dialog :title="title" :visible.sync="work" width="30%" style="text-align: left;top:20%">
            <span>操作成功</span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="work = false">确定</el-button>
            </span>
          </el-dialog>
        </el-collapse-item>
      </el-collapse>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      let validatePass = (rule, value, callback) => {
        if (this.form.password != '' && value === undefined) {
          callback(new Error('请再次输入密码'));
        } else if (this.form.password != '' && value != this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        //查询条件对象
        queryForm: {
          name: '',
          pageNum: 1,
          pageSize: 5
        },

        upData: {
          id: '',
        },
        activeName: "1",
        //数据表格数组
        tableData: [],
        //一共多少条数据
        total: 0,
        //对话框标题
        title: '',
        carImgUrl: '',
        //显示/隐藏对话框
        show: false,
        pic: false,
        work: false,
        delshow: false,
        //初始化表单对象
        form: {},
        action: '/api/car/car/uploadImg',
        headers: {
          Token: this.$store.state.token
        },
        //初始化检验对象
        rules: {
          name: [{
            required: true,
            message: '请输入车辆名称',
            trigger: 'blur'
          }],
          brand: [{
            required: true,
            message: '请输入车辆品牌',
            trigger: 'blur'
          }],
          model: [{
            required: true,
            message: '请输入车辆型号',
            trigger: 'blur'
          }],
          price: [{
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          }]
        },
        //厢数
        boxlist: [{
          id: '2',
          name: '2'
        }, {
          id: '3',
          name: '3'
        }],
        //门数
        doorlist: [{
          id: '2',
          name: '2'
        }, {
          id: '4',
          name: '4'
        }, {
          id: '5',
          name: '5'
        }],
        //座数
        seatlist: [{
          id: '2',
          name: '2'
        }, {
          id: '5',
          name: '5'
        }, {
          id: '6',
          name: '6'
        }, {
          id: '7',
          name: '7'
        }],
        //档位选择
        autolist: [{
          id: '自动',
          name: '自动'
        }, {
          id: '手动',
          name: '手动'
        }, {
          id: '一体',
          name: '一体'
        }],
        //状态
        statuslist: [{
          id: '正常',
          name: '正常'
        }, {
          id: '下架',
          name: '下架'
        }],
        //标识是新增还是修改
        dialogType: 'add',
        //主键是否可以被编辑:默认可以编辑
        disabled: false
      }
    },
    methods: {
      query() {
        this.$axios({
          method: 'post',
          url: 'api/car/car/list',
          data: this.queryForm
        }).then(res => {
          if (res.data.success) {
            this.tableData = res.data.obj.list;
            this.total = res.data.obj.total;
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(err => {
          console.info(err)
        });
      },
      openDel(row) {
        //设置对话框标题
        this.title = "车辆删除";
        //打开对话框
        this.delshow = true;

        this.id = row;

      },
      openAdd() {
        //设置对话框标题
        this.title = "车辆新增";
        //清空表单
        this.form = {};
        //主键可编辑
        this.disabled = false;
        //打开对话框
        this.show = true;
        //更改标识为新增
        this.dialogType = 'add';
        //清除表单校验结果
        this.$nextTick(function() {
          this.$refs['form'].clearValidate();
        });
      },
      openEdit(row) {
        //设置对话框标题
        this.title = "车辆修改";
        //清空表单
        this.form = {};
        //主键不可以编辑
        this.disabled = true;
        //打开对话框
        this.show = true;
        //更改标识为编辑
        this.dialogType = 'edit';
        //对列表行对象进行克隆，解决双向绑定问题 注意：select 需要 @change="$forceUpdate()"强制刷新
        this.form = Object.assign({}, row);
        //清除表单校验结果
        this.$nextTick(function() {
          this.$refs['form'].clearValidate();
        });
      },
      openPic(row) {

        this.$axios({
          method: 'post',
          url: 'api/car/car/image/' + row.id,
        }).then(res => {
          this.carImgUrl = res.data.obj.base64;
        }).catch(err => {
          console.info(err)
        });

        //设置对话框标题
        this.title = "上传图片";
        //打开对话框
        this.pic = true;

        this.upData.id = row.id;
      },
      del(row) {
        this.delshow = false;
        this.$axios({
          method: 'get',
          url: 'api/car/car/del/' + row.id
        }).then(res => {
          //刷新页面
          this.work = true;
          this.query()
        }).catch(err => {
          console.info(err)
        });
      },
      submitForm() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.dialogType == 'add') {
              this.$axios({
                method: 'post',
                url: 'api/car/car/add',
                data: this.form
              }).then(res => {
                //关闭对话框
                this.show = false;
                //刷新页面
                this.query()
                this.work = true;
              }).catch(err => {
                console.info(err)
              });
            } else {
              this.$axios({
                method: 'post',
                url: 'api/car/car/edit',
                data: this.form
              }).then(res => {
                //关闭对话框
                this.show = false;
                //刷新页面
                this.query();
                this.work = true;
                this.work = ture;
              }).catch(err => {
                console.info(err)
              });
            }
          }
        });
      },
      beforeUpload(file) {
        let types = ['image/jpeg'];
        const isImage = types.includes(file.type);
        const isLtSize = file.size / 1024 / 1024 < 5;
        if (!isImage) {
          this.$message({
            showClose: true,
            message: '上传图片只能是JPG格式!',
            type: 'error'
          });
          return false;
        }
        if (!isLtSize) {
          this.$message.error('上传图片大小不能超过 5MB!');
          return false;
        }
        return true;
      },
      submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemove(file) {
        this.$refs.upload.clearFiles();
      },
      uploadSuccess(response, file, fileList) {
        this.pic = false;
        this.work = true;
      },
      uploadError(err, file, fileList) {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        });
      }

    },
    // computed: {
    //   imageUrl() {
    //     return 'data:image/jpeg;base64,' + this.$store.state.carImgUrl;
    //   }
    // },

    created() {
      //初始化页面时进行数据查询
      this.query();
    }
  }
</script>

<style>
  .el-card {
    width: 100%;
  }


  .el-select {
    width: 100%;
  }
</style>
